import {
  getTicket,
} from '../services/tableau';

export default {
  namespace: 'tableau',
  state: {
    ticket: '',
    loading: true,
  },
  effects: {
    *getTicket({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(getTicket, payload);
      yield put({
        type: 'save',
        payload: { ticket: response, loading: false },
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
